package pages;


import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import utils.Log4Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class HomePage extends GeneralPage {


    public boolean isOpened(String URL) {
        return webDriver.getCurrentUrl().equals(URL);
    }




    public DetailPage openAdDetailPageWithPrivateImages(){
        Log4Test.info("Find Ad with private images and open it");
        elementIsLocated(getLocator("escortsLink")).click();
        List<WebElement> elements = webDriver.findElements(getLocator("listOfAds"));
        Iterator<WebElement> i = elements.iterator();
        List<String> elementsText = new ArrayList<String>();
        while (i.hasNext()){
            WebElement element = i.next();
            if(element.isEnabled()){
                String[] href = element.getAttribute ("href").split ("/");
                elementsText.add("/" + href [4] + "/");
                System.out.println("/" + href [4] + "/");
            }
        }
        Iterator<String> i2 = elementsText.iterator();
        while (i2.hasNext()){
            String text = i2.next();
            ((JavascriptExecutor)webDriver).executeScript("var link = $('a[href*=\"' + arguments[0] + '\"]'); if (link.length > 0) { link.click (); }", text);
            if (isElementPresent("unlockPrivatePhotoBtn")) {
                break;
            }
            webDriver.navigate().back();
        }
        return new DetailPage();
    }

}



